//
//  ExercisesAnswerResultScoreCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/11.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesAnswerResultScoreCell.h"

@interface ExercisesAnswerResultScoreCell ()
@property (nonatomic,strong)UILabel *paperLb;
@property (nonatomic,strong)UILabel *scoreLb;
@property (nonatomic,strong)UIImageView *scoreLine;
@property (nonatomic,strong)UILabel *totalScoreLb;
@property (nonatomic,strong)UILabel *realScoreLb;
@property (nonatomic,strong)UILabel *postTimeLb;
@property (nonatomic,strong)UILabel *useTimeLb;
@property (nonatomic,strong)UIImageView *line;
@property (nonatomic,strong)UILabel *cardLb;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *greenView;
@property (nonatomic,strong)UIView *redView;
@property (nonatomic,strong)UIView *grayView;
@property (nonatomic,strong)UILabel *greenLb;
@property (nonatomic,strong)UILabel *redLb;
@property (nonatomic,strong)UILabel *grayLb;
@property (nonatomic,strong)UIImageView *coverImgView;
@end

@implementation ExercisesAnswerResultScoreCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.contentView.backgroundColor = [UIColor clearColor];

    self.coverImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exercises_tag_cover"]];
    [self.contentView addSubview:self.coverImgView];
    [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.containView];

    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.right.equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    

    
    self.paperLb = [[UILabel alloc]init];
    self.paperLb.textColor = C35;
    self.paperLb.font = [UIFont boldSystemFontOfSize:26];
    self.paperLb.numberOfLines = 0;
    self.paperLb.textAlignment = NSTextAlignmentCenter;
    [self.containView addSubview:self.paperLb];

    [self.paperLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(16);
        make.right.equalTo(self.containView).offset(-16);
        make.top.equalTo(self.containView).offset(32);
    }];
    self.scoreLb = [[UILabel alloc]init];
    self.scoreLb.textColor = RGBA(221, 58, 49, 1);
    self.scoreLb.font = [UIFont boldSystemFontOfSize:40];
    [self.containView addSubview:self.scoreLb];
    [self.scoreLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.paperLb);
        make.top.equalTo(self.paperLb.mas_bottom).offset(24);

    }];
    
    self.scoreLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exercises_score_line"]];
    [self.containView addSubview:self.scoreLine];
    [self.scoreLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scoreLb);
        make.top.equalTo(self.scoreLb.mas_bottom);
    }];


////
    self.totalScoreLb = [[UILabel alloc]init];
    self.totalScoreLb.text = @"满分100分 [可作答90分]";
    self.totalScoreLb.textColor = C153;
    self.totalScoreLb.font = font(12);
    [self.containView addSubview:self.totalScoreLb];
    [self.totalScoreLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scoreLine.mas_bottom).offset(12);
        make.centerX.equalTo(self.paperLb);
    }];

    self.postTimeLb = [[UILabel alloc]init];
    self.postTimeLb.text = @"交卷时间：2020.07.12 13:41";
    self.postTimeLb.textColor = C102;
    self.postTimeLb.font = font(12);
    [self.containView addSubview:self.postTimeLb];
    [self.postTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.totalScoreLb.mas_bottom).offset(24);
        make.left.equalTo(self.contentView).offset(28);
    }];

    self.useTimeLb = [[UILabel alloc]init];
    self.useTimeLb.text = @"用时：12分50秒";
    self.useTimeLb.textColor = C102;
    self.useTimeLb.font = font(12);
    [self.containView addSubview:self.useTimeLb];
    [self.useTimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.totalScoreLb.mas_bottom).offset(24);
        make.right.equalTo(self.contentView).offset(-28);
    
    }];

    self.line = [[UIImageView alloc]init];
    [self.containView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.useTimeLb.mas_bottom).offset(24);
        make.left.equalTo(self.postTimeLb.mas_left);
        make.right.equalTo(self.useTimeLb.mas_right);
        make.height.mas_equalTo(2).priorityHigh();
    }];
//
//
    self.cardLb = [[UILabel alloc]init];
    self.cardLb.text = @"答题卡";
    self.cardLb.textColor = C35;
    self.cardLb.font = [UIFont boldSystemFontOfSize:18];
    [self.contentView addSubview:self.cardLb];
    [self.cardLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.line.mas_bottom).offset(24);
        make.left.equalTo(self.postTimeLb);
    }];


    self.greenView = [[UIView alloc]init];
    self.greenView.backgroundColor = RGBA(109, 221, 127, 1);
    [self.contentView addSubview:self.greenView];

    self.redView = [[UIView alloc]init];
    self.redView.backgroundColor = RGBA(255,100, 99, 1);
    [self.contentView addSubview:self.redView];


    self.grayView = [[UIView alloc]init];
    self.grayView.backgroundColor = RGBA(229, 229, 229, 1);
    [self.contentView addSubview:self.grayView];

    self.greenLb = [[UILabel alloc]init];
    self.greenLb.text = @"答对";
    self.greenLb.textColor = C102;
    self.greenLb.font = font(12);
    [self.contentView addSubview:self.greenLb];

    self.redLb = [[UILabel alloc]init];
    self.redLb.text = @"答错";
    self.redLb.textColor = C102;
    self.redLb.font = font(12);
    [self.contentView addSubview:self.redLb];

    self.grayLb = [[UILabel alloc]init];
    self.grayLb.text = @"无需作答";
    self.grayLb.textColor = C102;
    self.grayLb.font = font(12);
    [self.contentView addSubview:self.grayLb];


    [self.grayLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.useTimeLb);
        make.centerY.equalTo(self.cardLb);
    }];
    [self.grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.cardLb);
        make.right.mas_equalTo(self.grayLb.mas_left).offset(-8);
    }];
    [self.redLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.grayView.mas_left).offset(-8);
        make.centerY.equalTo(self.cardLb);
    }];
    [self.redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.cardLb);
        make.right.mas_equalTo(self.redLb.mas_left).offset(-8);
    }];
    [self.greenLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.redView.mas_left).offset(-8);
        make.centerY.equalTo(self.cardLb);
    }];
    [self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.cardLb);
        make.right.mas_equalTo(self.greenLb.mas_left).offset(-8);
    }];


    [self.scoreLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.scoreLine setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

    [self.totalScoreLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.useTimeLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];


}




- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
    // 开始划线 划线的frame
    UIGraphicsBeginImageContext(imageView.frame.size);

    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

    // 获取上下文
    CGContextRef line = UIGraphicsGetCurrentContext();

    // 设置线条终点的形状
    CGContextSetLineCap(line, kCGLineCapRound);
    // 设置虚线的长度 和 间距
    CGFloat lengths[] = {5,5};

    CGContextSetStrokeColorWithColor(line, RGBA(221, 221, 221, 1).CGColor);
    // 开始绘制虚线
    CGContextSetLineDash(line, 0, lengths, 2);

    CGContextMoveToPoint(line, 0.0, 2.0);

    CGContextAddLineToPoint(line, 300, 2.0);

    CGContextStrokePath(line);

    // UIGraphicsGetImageFromCurrentImageContext()返回的就是image
    return UIGraphicsGetImageFromCurrentImageContext();
}


- (void)drawRect:(CGRect)rect{
    self.line.image = [self drawLineOfDashByImageView:self.line];
//
//
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;

}
- (void)setModel:(ExercisesPaperResultModel *)model{
    self.paperLb.text = model.paper.name;
    self.scoreLb.text = model.answer.score;
    
    __block int realScore = 0;
    
    
    for (ExercisesTitleModel *obj in model.paper.titleItems) {
        realScore += obj.replyScore.intValue;
    }
    

    NSDate *date=  [NSDate dateWithTimeIntervalSince1970:(model.answer.createTime.longValue/1000)];
    NSDateFormatter *formatter= [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy.MM.dd HH:mm";
    
    
    int seconds = model.answer.doTime.intValue % 60;

    int minutes = (model.answer.doTime.intValue / 60) % 60;

    int hours = model.answer.doTime.intValue / 3600;



    self.postTimeLb.text = [NSString stringWithFormat:@"交卷时间：%@",[formatter stringFromDate:date]];
    
    if (model.answer.doTime.intValue < 60) {
        self.useTimeLb.text = [NSString stringWithFormat:@"用时：%@秒",model.answer.doTime];
    }
    if (model.answer.doTime.intValue > 60 && model.answer.doTime.intValue < 3600) {
        
        int seconds = model.answer.doTime.intValue % 60;

        int minutes = (model.answer.doTime.intValue / 60) % 60;
        
        self.useTimeLb.text = [NSString stringWithFormat:@"用时：%i分%i秒",minutes,seconds];
    }
    if (model.answer.doTime.intValue > 3600) {
        self.useTimeLb.text = [NSString stringWithFormat:@"用时：%i时%i分%i秒",hours,minutes,seconds];
    }
    
    self.totalScoreLb.text = [NSString stringWithFormat:@"满分%li分 [可作答%i分]",model.paper.totalScore,realScore];
}



@end
